//
//  KLNetWorkTool.m
//  AFNDemo
//
//  Created by 梁家伟 on 17/3/15.
//  Copyright © 2017年 itcast. All rights reserved.
//

#import "QDNetworkTool.h"

@interface QDNetworkTool ()

@end

@implementation QDNetworkTool

+(instancetype)sharedTool{
    
    static QDNetworkTool* tool = nil;
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        tool = [[QDNetworkTool alloc]init];
        
        //告诉AFN返回原始数据
//        tool.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        //告诉服务器发送的是json及手动添加可接受类型
        tool.requestSerializer = [AFHTTPRequestSerializer serializer];
        tool.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/html",@" text/plain", nil];
        
    });
    return tool;
}

-(void)requestWithHTTPMethod:(NSString *)method
                                     URLString:(NSString *)URLString
                                    parameters:(id)parameters
                                CallBack:(nullable void (^)(id)) callBack{
    

    
    if([method isEqualToString:@"GET"]){
        
        [self GET:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            callBack(responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
             callBack(nil);
        }];
        
    }else if([method isEqualToString:@"POST"]){
        
        [self POST:URLString parameters:parameters progress: nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            callBack(responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"error");
            callBack(nil);
        }];
    }

}

@end
